/*
 * 
 * Copyright 2011 visual-analysis-of-cancer.
 * This file is part of visual-analysis-of-cancer.
 * 
 * visual-analysis-of-cancer is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * visual-analysis-of-cancer is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with kOSMik.  If not, see <http://www.gnu.org/licenses/>.
 *  
 * visual-analysis-of-cancer:
 * An tool to analye the WHO and GLOBOCAN cancer data.
 * http://code.google.com/p/visual-analysis-of-cancer/
 * 
 * 
 * 
 * Authors: Erik Sniegula, Johannes Dahlke
 * 
 * Module:  Programming in Processing for Visual Analytics
 * Group:   20
 * 
 * Created: Aug. 2011
 * Version: 1.0
 *
 */
 




package de.fuberlin.processing.who.cancer.controlp5ex;

import java.awt.Color;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.ListResourceBundle;
import java.util.Vector;


import processing.core.PApplet;
import controlP5.Button;
import controlP5.CColor;
import controlP5.ControlCanvas;
import controlP5.ControlEvent;
import controlP5.ControlGroup;
import controlP5.ControlP5;
import controlP5.ControlP5Constants;
import controlP5.ControllerGroup;
import controlP5.ControllerInterface;
import controlP5.ListBox;
import controlP5.ListBoxItem;
import controlP5.Slider;
import de.fuberlin.processing.who.cancer.gui.Scatterplot;
import de.fuberlin.processing.who.cancer.utils.Fonts;
import de.fuberlin.processing.who.cancer.utils.Graphics;
import de.fuberlin.processing.who.cancer.utils.Test;


public class ExpandableControlGroup extends ControlGroup {

	private int expandedHeight;
	private int shrunkenHeight;
	private int left;
	private int top;
	private boolean isExpanded = false;
	private ControlGroup thisControlGroup;
	private final Button moreButton; 
	

	public ExpandableControlGroup( ControlP5 theControlP5, ControllerGroup  theParent,
			String theName, int theX, int theY, int theW, int theH, int theExpandedH) {
		super( theControlP5, theParent, theName, theX, theY, theW, theH);
	  	
		this.shrunkenHeight = theH;
		this.expandedHeight = theExpandedH;
				
    this.top = theX;
    this.left = theY;
    thisControlGroup = this;


    
    moreButton = new Button( controlP5, this, this._myName + "MoreButton", 0, 0, getHeight() -16, getWidth(), 16) {
    	
    	@Override
    	public void mousePressed() {
    		isExpanded = !isExpanded;
    		thisControlGroup.setHeight( isExpanded ? expandedHeight : shrunkenHeight);
    		if ( isExpanded)
    			onExpand(); else
    			onShrink();	
    		super.mousePressed();
    	}
    	
    	@Override
    	public void draw( PApplet p) {
    		p.pushStyle();
				p.fill( getColor().getBackground());
				p.rect( position.x, position.y, getWidth(), getHeight());
				p.textFont( Fonts.getDejavuSans(), 10);
				p.fill( getColor().getCaptionLabel());
				
				float left = position.x;
				float top = position.y;
				
				if ( !isExpanded) {
				  p.text( "MORE" ,  position.x + getWidth() -60,  position.y + getHeight() -4);
				  p.triangle( left + getWidth() -11, top + getHeight() -9, left + getWidth() -5, top + getHeight() -9, left + getWidth() -8, top + getHeight() -5);
				} else {
				  p.text( "MINIMIZE" ,  position.x + getWidth() -80,  position.y + getHeight() -4);
				  p.triangle( left + getWidth() -11, top + getHeight() -5, left + getWidth() -5, top + getHeight() -5, left + getWidth() -8, top + getHeight() -9);
				}
				p.popStyle();
    	}
    };
    moreButton.addListener( this);
    this.add( moreButton);
    // update buttons visibility
		setExpandedHeight( theExpandedH);
   
    
    this.addCanvas( new ControlCanvas() {
			
    	@Override
			public void draw( PApplet p) {
				p.pushStyle();
				//p.fill( thisControlGroup.getColor().getBackground());
				p.fill( getColor().getForeground());
				p.noStroke();
				int width = thisControlGroup.getWidth();
				int height = thisControlGroup.getHeight();
				p.rect( 0, 0, width, height);
				p.stroke( getColor().getBackground());
				p.strokeWeight( 2);				
				p.line( 0, 0, width, 0);
				p.line( width, 0, width, height-2);
				p.line( width -Scatterplot.NUM_CELL_WIDTH, height-2, width, height-2);
				p.line( 0, 0, 0, height-2);
				p.popStyle();
		  }
		});
    
	}
	
	
	public void onExpand() {}
	
	public void onShrink() {}
	
	
	public int getVisibleBoxHeight() {
		return isExpandable() ? ( isExpanded ? expandedHeight -16 : shrunkenHeight -16) : shrunkenHeight; 
	}
	
	
	public boolean isExpandable() {
		return expandedHeight - shrunkenHeight > 0;
	}
	
	@Override
	public ControllerGroup setHeight( int theHeight) {
		int diffHeight = expandedHeight - shrunkenHeight;
		moreButton.getPosition().y = isExpanded ? expandedHeight -16 : shrunkenHeight -16; 
		return super.setHeight( theHeight);
	}
	
	public void setExpandedHeight( int expandedHeight) {
		this.expandedHeight = expandedHeight;
		moreButton.setVisible( isExpandable());
		if ( isExpanded)
			this.setHeight( expandedHeight);
		this.update();
	}
	
	
	

	

	

}